package com.zretc.blog.service;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.zretc.api.pojo.Comment;
import com.zretc.blog.dao.CommentMapper;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class CommentService {
   @Autowired
    private CommentMapper commentMapper;
   @Autowired
   StringRedisTemplate stringRedisTemplate;
   @Autowired
    RocketMQTemplate rocketMQTemplate;

    public void addComment(Comment comment){
        String key = "Comment" + comment.getBlogId();
        //Long size = stringRedisTemplate.opsForList().size(key);
        stringRedisTemplate.delete(key);//.rightPushAll(key,JSON.toJSONString(comment));

        Integer id = comment.getCommentatorId();
        Map<String,Object> map= new HashMap<>();
        map.put("userId",id);
        map.put("value",3);
        rocketMQTemplate.syncSend("credit", map);
        commentMapper.addComment(comment);

    };
    public List<Comment> getCommentList(Integer blogId){
        String key = "Comment" + blogId;
        Long size = stringRedisTemplate.opsForList().size(key);
        List<String> list_string;      //写入redis的表
        List<Comment> list;           //返回Comment对象的表
        if(size != 0){
            list_string = stringRedisTemplate.opsForList().range(key,0,size);
            list = list_string.stream().map(s -> {
                Comment comment = JSON.parseObject(s,Comment.class);
                return comment;
            }).collect(Collectors.toList());
            System.out.println(list_string);
            return list;//缓存中存在，直接返回
        }else{
            list = commentMapper.getCommentList(blogId);
            if(list.isEmpty()){
                return null;
            }
            //缓存中不存在，从数据库读取
            list_string = list.stream().map(comment -> {
                return JSON.toJSONString(comment);
            }).collect(Collectors.toList());
            //System.out.println(list_string);
            stringRedisTemplate.opsForList().rightPushAll(key, list_string);
            // List<Comment> list = commentMapper.getCommentList(blogId);
            return list;
        }

    }
    public List<Comment> getCommentByUser(Integer commentatorId){
        ArrayList<Comment> list = commentMapper.getCommentByUser(commentatorId);
        return list;
    }
    public void deleteComment(Integer id){
        Comment comment = commentMapper.getCommentById(id);
        Integer bid = comment.getBlogId();
        String key ="Comment" + String.valueOf(bid);
        Long size = stringRedisTemplate.opsForList().size(key);
        //System.out.println(blogJson);
        if(size != 0){
            stringRedisTemplate.delete(key);
        }
        commentMapper.deleteComment(id);
    }
    public void setCommentPass(Comment comment) {
        commentMapper.setCommentPass(comment);
    }
    public void setCommentDeleted(Comment comment){
        commentMapper.setCommentDeleted(comment);
    }
    public void deleteCommentByBlog(Integer blogId){
        commentMapper.deleteCommentByBlog(blogId);
    }
    public void likeComment(Integer id){
        commentMapper.likeComment(id);
    }
    public Comment getCommentById(Integer id){
        Comment comment = commentMapper.getCommentById(id);
        return comment;
    }

}
